//
//  ICGroupUserController.m
//  ICome
//
//  Created by ENN on 16/6/6.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICGroupUserController.h"
#import "ICContactListCell.h"
#import "ICDetailSearchController.h"
#import "UIImage+AvatarGeneration.h"
#import "iComeApiManager.h"

@interface ICGroupUserController ()<UISearchControllerDelegate,UISearchBarDelegate,UITableViewDelegate, UITableViewDataSource,DetailSearchDelegate>

@property (nonatomic, strong) ICBaseSearchViewController *searchController;

@property (nonatomic, strong)UITableView *tableView;

@end

@implementation ICGroupUserController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"成员";
    [self setupUI];
}


- (void)setupUI
{
    ICDetailSearchController *searchVC   = [[ICDetailSearchController alloc] init];
    searchVC.delegate                    = self;
    searchVC.gId                         = self.gId;
    
    ICBaseSearchViewController *searchController = [[ICBaseSearchViewController alloc] initWithSearchResultsController:searchVC];
    searchController.placeholder = @"搜索";
    searchController.delegate = self;
    searchController.searchBar.delegate  = self;
    searchController.searchResultsUpdater = searchVC;
    self.definesPresentationContext = YES;
    _searchController = searchController;  // 强指针引用
    self.tableView.tableHeaderView = searchController.searchBar;

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom);
    }];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.users.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *res = @"cell";
    ICContactListCell *cell = [tableView dequeueReusableCellWithIdentifier:res];
    if (!cell) {
        cell = [[ICContactListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:res];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ICUser *model = [self.users objectAtIndex:indexPath.row];
    cell.titleLabel.text = model.eName;
    cell.isMaster = [model.eId isEqualToString:self.master];
    NSString *URL = [ICTools getMinImageURL:(model.photoId)];
      [UIImage addAvatariamgeView:cell.headImageView withUrlStr:URL witheId:model.eId withName:model.eName];
//    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:URL] placeholderImage:[UIImage imageNamed:@"App_personal_headimg"]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55.0f * Level;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ICUser *user = self.users[indexPath.row];
    NSDictionary *param = @{@"eId":user.eId};
    if ([user.eId rangeOfString:@"bot"].location == NSNotFound) {
        if ([[iComeApiManager instance].delegate respondsToSelector:@selector(openContactsDetailsFromViewC::)]) {
            [[iComeApiManager instance].delegate openContactsDetailsFromViewC:self:param];
        } else {
            UIViewController * vc = [[IComMediator sharedInstance] IComMediator_ContactsDetailViewController:param];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else
    {
        UIViewController *vc = [[IComMediator sharedInstance] IComMediator_RobotDetailViewController:param];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - searchBarDelegate

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES];
    for(UIView *view in  [[[searchBar subviews] objectAtIndex:0] subviews]) {
        if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
            UIButton * cancel =(UIButton *)view;
            [cancel setTitle:@"取消" forState:UIControlStateNormal];
            [cancel setTitleColor:ICSEARCHCANCELCOLOR forState:UIControlStateNormal];
        }
    }
}

- (void)detailSearch:(NSString *)eId
{
    NSDictionary *param = @{@"eId":eId};
    if ([eId rangeOfString:@"bot"].location == NSNotFound) {
        if ([[iComeApiManager instance].delegate respondsToSelector:@selector(openContactsDetailsFromViewC::)]) {
            [[iComeApiManager instance].delegate openContactsDetailsFromViewC:self:param];
        } else {
            UIViewController * vc = [[IComMediator sharedInstance] IComMediator_ContactsDetailViewController:param];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else
    {
        UIViewController *vc = [[IComMediator sharedInstance] IComMediator_RobotDetailViewController:param];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if (self.searchController.active) {
        self.searchController.active = NO;
        [self.searchController.searchBar removeFromSuperview];
    }
}


- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.userInteractionEnabled = YES;
        _tableView.backgroundColor = BACKGROUNDCOLOR;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

@end
